package com.ruoyi.back.controller;

import java.util.List;

import com.ruoyi.back.domain.Curriculum;
import com.ruoyi.back.service.ICurriculumService;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.common.utils.uuid.SnowflakeIdUtils;
import io.swagger.annotations.*;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 * 
 * @author luguanqing
 * @date 2021-04-06
 */
@Api(value="课程信息",description = "课程信息接口")
@RestController
@RequestMapping("/back/curriculum")
public class CurriculumController extends BaseController
{
    @Autowired
    private ICurriculumService curriculumService;

    /**
     * 查询后台课程信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:list')")
    @ApiOperation(value="课程后台信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    @GetMapping("/listback")
    public TableDataInfo listback(Curriculum curriculum)
    {
        startPage();
        List<Curriculum> listback = curriculumService.selectCurriculumList(curriculum);
        return getDataTable(listback);
    }

    /**
     * 查询前台课程信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:list')")
    @ApiOperation(value="课程前台信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    @GetMapping("/list")
    public TableDataInfo list(Curriculum curriculum)
    {
        startPage();
        List<Curriculum> list = curriculumService.selectCurriculumList(curriculum);
        for(Curriculum curriculums : list){
            curriculums.setUrl("");
            curriculums.setShorturl("");
        }
        return getDataTable(list);
    }


    /**
     * 导出课程信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Curriculum curriculum)
    {
        List<Curriculum> list = curriculumService.selectCurriculumList(curriculum);
        ExcelUtil<Curriculum> util = new ExcelUtil<Curriculum>(Curriculum.class);
        return util.exportExcel(list, "curriculum");
    }

    /**
     * 获取课程信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:query')")
    @ApiOperation(value="获取课程详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(name="id",value="课程详情id",required=true)@PathVariable("id") String id)
    {
        Curriculum curriculum = curriculumService.selectCurriculumById(id);
        curriculum.setShorturl("");
        curriculum.setUrl("");
        return AjaxResult.success(curriculum);
    }

    /**
     * 获取完整视频
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:query')")
    @ApiOperation(value="获取完整视频")
    @GetMapping(value = "getVideoUrl/{id}")
    public AjaxResult getVideoUrl(@ApiParam(name="id",value="课程详情id",required=true)@PathVariable("id") String id)
    {
        String videoUrl = curriculumService.selectCurriculumById(id).getUrl();
        return AjaxResult.success(videoUrl);
    }

    /**
     * 获取5分钟视频
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:query')")
    @ApiOperation(value="获取5分钟视频")
    @GetMapping(value = "getShortVideoUrl/{id}")
    public AjaxResult getShortVideoUrl(@ApiParam(name="id",value="课程详情id",required=true)@PathVariable("id") String id)
    {
        String videoUrl = curriculumService.selectCurriculumById(id).getShorturl();
        return AjaxResult.success(videoUrl);
    }

    /**
     * 新增课程信息
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:add')")
    @ApiOperation(value="新增课程信息")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(name="课程对象",value="传入json格式",required=true)@RequestBody Curriculum curriculum)
    {
        curriculum.setId(String.valueOf(new SnowflakeIdUtils(0, 0).nextId()));
        return toAjax(curriculumService.insertCurriculum(curriculum));
    }

    /**
     * 修改课程信息
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:edit')")
    @ApiOperation(value="修改课程信息")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(name="课程对象",value="传入json格式",required=true)@RequestBody Curriculum curriculum)
    {
        return toAjax(curriculumService.updateCurriculum(curriculum));
    }

    /**
     * 课程访问记录+1
     */
    //@PreAuthorize("@ss.hasPermi('system:information:edit')")
    @ApiOperation(value="课程访问记录+1")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/visitUp/{id}")
    public AjaxResult visitUp(@ApiParam(name="id",value="课程id",required=true)@PathVariable("id") String id)
    {
        return toAjax(curriculumService.updateVisit(id));
    }

    /**
     * 课程订购记录+1
     */
    //@PreAuthorize("@ss.hasPermi('system:information:edit')")
    @ApiOperation(value="课程订购记录+1")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/orderUp/{id}")
    public AjaxResult orderUp(@ApiParam(name="id",value="课程id",required=true)@PathVariable("id") String id)
    {
        return toAjax(curriculumService.updateOrder(id));
    }

    /**
     * 删除课程信息
     */
    //@PreAuthorize("@ss.hasPermi('system:curriculum:remove')")
    @ApiOperation(value="删除课程信息")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(name="id",value="课程id",required=true)@PathVariable String[] ids)
    {
        return toAjax(curriculumService.deleteCurriculumByIds(ids));
    }
}
